<?php
namespace Home\Controller;
class CampController extends HomeController {
	public function _initialize() {
        parent::_initialize();
        $this->_cate_mod = D('ArticleCate');
        $this->_mod = D('Article');
        $this->default_cate_id = 16;
    }
    public function _empty($title){
        $this->category_page(0,$title);
    }
    public function index($id = 0,$alias=''){
        $cate_id = empty($id)?I('id',0,'intval'):$id;
        if(!$cate_id && $alias){
            $cate_id = $this->_cate_mod->where(array('alias'=>$alias))->getField('id');
        }
        $cate_id = empty($cate_id)?$this->default_cate_id:$cate_id;
        (!$cate_id) && $this->error('参数有误');
        //当前栏目信息
        $cate = $this->_cate_mod->find($cate_id);
        //子栏目列表
        if($cate['spid'] == 0){
            $sub_cate = $this->_cate_mod->where(array('pid'=>$cate_id))->order('ordid desc,id desc')->select();
            $big_cate = $cate;
        } else{
            $pid = current(explode('|',$cate['spid']));
            $sub_cate = $this->_cate_mod->where(array('pid'=>$pid))->order('ordid desc,id desc')->select();
            $big_cate = $this->_cate_mod->find($pid);
        }

        if ($cate_id) {
            $id_arr = $this->_cate_mod->get_child_ids($cate_id, true);
            $map['cate_id'] = array('IN', $id_arr);
        }
        //分页
        $count = $this->_mod->where($map)->count();
        $Page = new \Think\Page($count,C('pagesize'));
        $list = $this->_mod->where($map)->field('id,title,add_time,intro')->limit($Page->firstRow.','.$Page->listRows)->relation('tag')->order('ordid desc,id desc')->select();
        $show = $Page->show();

        $this->assign('sub_cate',$sub_cate);
        $this->assign('cate',$cate);
        $this->assign('big_cate',$big_cate);
        $this->assign('list',$list);
        $this->assign('page',$show);
        $templet = empty($cate['index_templet'])?'index':$cate['index_templet'];
        $this->display($templet);
    }

    public function category($id = 0,$alias=''){
        $cate_id = empty($id)?I('id',0,'intval'):$id;
		$cate_id = empty($cate_id) ? 47:$cate_id;
        (!$cate_id) && $this->error('参数有误');
		$info = $this->_mod->find($cate_id);
        //当前栏目信息
        $cate = $this->_cate_mod->find($cate_id);
        //
		if($cate['type']){
			$this->category_page($cate_id);
			exit();
		}
        //子栏目列表
        if($cate['spid'] == 0){
            $sub_cate = $this->_cate_mod->where(array('pid'=>$cate_id))->select();
            $big_cate = $cate;
        }else{
            $pid = current(explode('|',$cate['spid']));
            $sub_cate = $this->_cate_mod->where(array('pid'=>$pid))->select();
            $big_cate = $this->_cate_mod->find($pid);
        }

        if ($cate_id) {
            $id_arr = $this->_cate_mod->get_child_ids($cate_id, true);
            $map['cate_id'] = array('IN', $id_arr);
        }
        //分页
        $count = $this->_mod->where($map)->count();
        $Page = new \Think\Page($count,C('pagesize'));
        $list = $this->_mod->where($map)->field('id,title,add_time,intro,img')->limit($Page->firstRow.','.$Page->listRows)->relation('tag')->order('ordid desc,id desc')->select();
        $show = $Page->show();
	
		$this->assign('info',$info);
        $this->assign('sub_cate',$sub_cate);
        $this->assign('cate',$cate);
        $this->assign('big_cate',$big_cate);
        $this->assign('list',$list);
        $this->assign('page',$show);

        $templet = empty($cate['category_templet'])?'category':$cate['category_templet'];
        $this->display($templet);
    }

    public function detail(){
        $id = I('id',0,intval);
        (!$id) && $this->error('参数有误');
        $info = $this->_mod->find($id);
        $cate_id = $info['cate_id'];
        //当前栏目信息
        $cate = $this->_cate_mod->find($cate_id);
        //子栏目列表
        if($cate['spid'] == 0){
            $sub_cate = $this->_cate_mod->where(array('pid'=>$cate_id))->order('ordid desc,id desc')->select();
            $big_cate = $cate;
        }else{
            $pid = current(explode('|',$cate['spid']));
            $sub_cate = $this->_cate_mod->where(array('pid'=>$pid))->order('ordid desc,id desc')->select();
            $big_cate = $this->_cate_mod->find($pid);
        }

        $id_arr = $this->_cate_mod->get_child_ids($cate_id, true);

        //前一篇和下一篇
        $map['status'] = array('eq',1);
        $map['cate_id'] = array('IN', $id_arr);

        $map['id'] = array('lt',$id);
        $ex = $this->_mod->field('id,title')->where($map)->order('id desc')->find();

        $map['id'] = array('gt',$id);
        $next = $this->_mod->field('id,title')->where($map)->order('id asc')->find();

        $this->assign('cate',$cate);
        $this->assign('info',$info);
        $this->assign('ex',$ex);
        $this->assign('next',$next);
        $this->assign('sub_cate',$sub_cate);
        $this->assign('big_cate',$big_cate);
        $this->display();
    }

	public function category_page($id=0,$alias=''){
        $cate_id = I('cate_id',0,'intval');
		$info_id = I('info_id',0,'intval');
        $cate_id = empty($cate_id) ? $id:$cate_id;
        if(!$cate_id && $alias){
            $cate_id = $this->_cate_mod->where(array('alias'=>$alias))->getField('id');
        }
        $list = $this->_mod->field('id,title,short_title,cate_id')->where(array('cate_id'=>$cate_id))->order('ordid desc,id asc')->select();
        $info_id = empty($info_id)?$list[0]['id']:$info_id;
        $info = $this->_mod->find($info_id);
        //当前栏目信息
        $cate = $this->_cate_mod->find($cate_id);
        //子栏目列表
        if($cate['spid'] == 0){
            $sub_cate = $this->_cate_mod->where(array('pid'=>$cate_id))->order('ordid desc,id desc')->select();
            $big_cate = $cate;
        }else{
            $pid = current(explode('|',$cate['spid']));
            $sub_cate = $this->_cate_mod->where(array('pid'=>$pid))->order('ordid desc,id desc')->select();
            $big_cate = $this->_cate_mod->find($pid);
        }

        $id_arr = $this->_cate_mod->get_child_ids($cate_id, true);

        //前一篇和下一篇
        $map['status'] = array('eq',1);
        $map['cate_id'] = array('IN', $id_arr);

        $map['id'] = array('lt',$info_id);
        $ex = $this->_mod->field('id,title')->where($map)->order('id desc')->find();

        $map['id'] = array('gt',$info_id);
        $next = $this->_mod->field('id,title')->where($map)->order('id asc')->find();

        $this->assign('list',$list);
        $this->assign('cate',$cate);
        $this->assign('info',$info);
        $this->assign('ex',$ex);
        $this->assign('next',$next);
        $this->assign('sub_cate',$sub_cate);
        $this->assign('$big_cate',$big_cate);

        $templet = empty($cate['category_templet'])?'category_page':$cate['category_templet'];
        $this->display($templet);
    }

}